package util;

import java.util.Vector;

import ns2.Event_Type;
import ns2.Field;
import ns2.Trace;

public class Traces extends Vector<Trace> {

	private static final long serialVersionUID = 1L;

	public void addTrace(Trace t) {
		if (t.event == Event_Type.Receive) {
			this.addElement(t);
		}
	}

	public void addTraces(Traces ts) {
		for (int i = 0; i < ts.size(); i++) {
			this.addTrace(ts.traceAt(i));
		}
	}

	public Trace traceAt(int index) {
		return this.elementAt(index);
	}

	public Vector<Traces> sort(Field field) {
		Vector<Traces> results = new Vector<Traces>();
		Vector<Integer> reference = new Vector<Integer>();
		Integer nodeID = new Integer(65535);
		for (int i = 0; i < this.size(); i++) {
			switch (field) {
			case Src_addr:
				nodeID = new Integer(this.traceAt(i).src_addr.nodeID);
				break;
			case Dst_addr:
				nodeID = new Integer(this.traceAt(i).dst_addr.nodeID);
				break;
			default:
				break;
			}
			if ((nodeID.intValue() != 65535) && (!reference.contains(nodeID))) {
				reference.addElement(nodeID);
				results.addElement(new Traces());
			}
			int index = reference.indexOf(nodeID);
			results.elementAt(index).addTrace(this.traceAt(i));
		}
		return results;
	}
}
